package com.cbs.idea.plugin.cbsdp;

import com.intellij.ide.util.PropertiesComponent;
import com.intellij.openapi.components.ApplicationComponent;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

/**
 * Created by broche on 12/22/15.
 */
public class DpComponent implements ApplicationComponent, Configurable {
    private Calculator calculator = new Calculator();
    private String template = "{n}*1080/1242/3";

    private JTextField jTextField;

    @Override
    public void initComponent() {
        template = PropertiesComponent.getInstance().getValue("template", "{n}*1080/1242/3");
        // TODO: insert component initialization logic here
    }

    @Override
    public void disposeComponent() {
        // TODO: insert component disposal logic here
    }

    @Override
    @NotNull
    public String getComponentName() {
        return "CBSDpCalculator";
    }


    public String convertDp(String input) {
        if (input == null || input.equals("")) {
            return "";
        }
        String valstr;
        String unit = "";
        if ((input.endsWith("dp") || input.endsWith("px") || input.endsWith("sp")) && input.length() > 2) {
            unit = input.substring(input.length() - 2, input.length());
            valstr = input.substring(0, input.length() - 2);
        } else {
            valstr = input;
        }
        Double valdbl;
        try {
            valdbl = Double.parseDouble(valstr);
        } catch (NumberFormatException e) {
            return "";
        }
        if (valdbl == 0) {
            return "";
        } else {
            String exp = template.replace("{n}", valdbl + "");
            int resint = (int) Math.round(calculator.calculate(exp));
            String resstr;
            if (!unit.equals("")) {
                resstr = resint + unit;
            } else {
                resstr = resint + "dp";
            }
            return resstr;
        }
    }

    // Configurable
    @Nls
    @Override
    public String getDisplayName() {
        return "CBSDpCalculator";
    }

    @Nullable
    @Override
    public String getHelpTopic() {
        return null;
    }

    @Nullable
    @Override
    public JComponent createComponent() {
        JComponent jComponent = new JPanel(null);
        JLabel jLabel1 = new JLabel("说明: 使用{n}代替输入参数, 例: {n}*2/3+(2-1)");
        jLabel1.setBounds(10, 10, 1024, 30);
        jComponent.add(jLabel1);
        JLabel jLabel2 = new JLabel("公式: ");
        jLabel2.setBounds(10, 40, 50, 30);
        jComponent.add(jLabel2);
        jTextField = new JTextField();
        jTextField.setBounds(60, 40, 256, 30);
        jTextField.setText(template);
        jComponent.add(jTextField);
        return jComponent;
    }

    @Override
    public boolean isModified() {
        return !jTextField.getText().equals(template);
    }

    @Override
    public void apply() throws ConfigurationException {
        template = jTextField.getText();
        PropertiesComponent.getInstance().setValue("template", template);

    }

    @Override
    public void reset() {
        jTextField.setText(template);
    }

    @Override
    public void disposeUIResources() {

    }
}
